﻿

IncludePath "Source/"
IncludeFile "threeD.pb"
IncludePath "/"

EnableExplicit

;{ Globals
	Global ShaderID.l
	Global Mesh.l
	Global hDC.l
	Global MatProj.mat4
	Global MatModel.mat4
;}
;{ Procedures
	Procedure UpdateModelMat()
		Protected.vec4 pos, Q
		Protected.mat3 mRot, mRot0, mPos
		Protected.f angle
		Protected.vec3 vAxe
		Vec3_Set(vAxe, 1, 1, 1)
		Vec3_Normalize(vAxe)
		angle = timeGetTime_()/1000
		Quat_FromAxis(Q, vAxe, angle)
		Mat3_FromQuat(mRot, Q)
		Vec4_Set(pos, 0, 0, -3, 1)
		Mat4_SetM(MatModel, mRot, pos)
	EndProcedure
	Procedure Init()
		UsePNGImageDecoder()
		OpenWindow(0, 100, 200, 512, 512, "PureBasic Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget|#PB_Window_ScreenCentered)
		hDC = CreateOpenGLContext(WindowID(0))		
		ShaderID = LoadShadersFromFiles("Data/ex03_shader.vs", "Data/ex03_shader.fs")
		Mesh = CreateTorusMesh(1/3, 2/3, 16*4, 32*4)
		UpdateModelMat()
	EndProcedure
	Procedure RenderScene()
		Protected.l uniformLocation
		glUseProgram(shaderID)
		uniformLocation = glGetUniformLocation(shaderID, @"u_mProj")
		If uniformLocation <> -1
			glUniformMatrix4fv(uniformLocation, 1, #True, @MatProj)
		EndIf
		uniformLocation = glGetUniformLocation(shaderID, @"u_mModel")
		If uniformLocation <> -1
			glUniformMatrix4fv(uniformLocation, 1, #True, @MatModel)
		EndIf		
		glEnable_(#GL_DEPTH_TEST)
		DrawMesh(Mesh)
	EndProcedure
	Procedure RunStart()
		Protected.l Event, Quit
		Repeat
			Event = WaitWindowEvent(1)
			Select Event
				Case #PB_Event_CloseWindow	
					Quit = 1
				Case #PB_Event_SizeWindow
					glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
					Mat4_CreatePerspective(MatProj, #PI/4, WindowWidth(0)/WindowHeight(0), 1/10, 10)
			EndSelect
			glClearColor_(0.3,0.3,0.3,1)
			glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
			UpdateModelMat()
			RenderScene()
			SwapBuffers_(hDC)
		Until Quit = 1
	EndProcedure
;}



Init()
RunStart()


























; IDE Options = PureBasic 5.11 (Windows - x86)
; CursorPosition = 58
; FirstLine = 27
; Folding = --
; EnableXP
; EnableCompileCount = 308
; EnableBuildCount = 0
; EnableExeConstant